package com.noble.meether.mail.ui.friend;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.bean.center.user.light.UserLightList;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.library.view.custom.state.CustomStatusListView;
import com.noble.library.widget.ListView.xlistview.ExListView;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;
import com.noble.meether.chat.unread.UnreadMgrImpl;
import com.noble.meether.chat.unread.view.BadgeView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 我的好友
 */
public class MyFriendsAct extends BaseAppCompatActivity implements RequestComplete, ExListView.IXListViewListener, View.OnClickListener, PObserver {
    private CustomStatusListView customStatusListView;
    private ExListView exListView;

    private MyFriendsAdapter adapter;
    private ArrayList<UserLight> data = new ArrayList<>();

    private int page = 1;
    private BadgeView badgeView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_friend_act);
        initTitle();
        initView();
        onRefresh();
    }

    private void initTitle() {
        setBackView();
        setTitle(getResources().getString(R.string.my_friend_title));
    }

    private void initView() {
        customStatusListView = findViewById(R.id.myfriend_list);
        badgeView = findViewById(R.id.mail_number);
        findViewById(R.id.rl_item).setOnClickListener(this);
        exListView = customStatusListView.getExListView();
        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(false);
        exListView.setPullLoadEnable(false);
        adapter = new MyFriendsAdapter(this, data);
        exListView.setAdapter(adapter);
        customStatusListView.showExListView();
        initBadgeView();
        MsgMgr.getInstance().attach(this);
    }

    /**
     * 好友申请角标
     * <p>
     * 存储Key为0
     */
    private void initBadgeView() {
        ModuleMgr.getUnreadMgr().registerMsgBadge(badgeView, false, 0);
    }

    @Override
    public void onRefresh() {
        page = 1;
        ModuleMgr.getCommonMgr().getMyFriends(page, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getCommonMgr().getMyFriends(page, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.isOk()) {
            UserLightList infoList = (UserLightList) response.getBaseData();

            if (infoList.getLightList().size() != 0) {
                if (page == 1) {
                    if (data.size() != 0) {
                        data.clear();
                    }
                }
                data.addAll(infoList.getLightList());
                if (data.size() < 10) {
                    exListView.setPullLoadEnable(false);
                } else {
                    exListView.setPullLoadEnable(true);
                }
                adapter.notifyDataSetChanged();
            } else {
                if (page == 1) {
                    data.clear();
                    adapter.notifyDataSetChanged();
                } else {
                    exListView.setPullLoadEnable(false);
                }
            }

            if (page == 1) {
                exListView.stopRefresh();
            } else {
                exListView.stopLoadMore();
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_item:
                MeetUIShow.showMyFriendRequestAct(this);
                resetFriendUnread();  // 处理未读角标
                break;
        }
    }

    private void resetFriendUnread() {
        int onlineNum = ModuleMgr.getUnreadMgr().resetMsgUnreadByKey(0);
        ModuleMgr.getUnreadMgr().reduceUnreadByKey(UnreadMgrImpl.MY_FRIEND, onlineNum);
        badgeView.setVisibility(View.GONE);
    }

    private boolean isRefresh = false;      // 是否刷新列表

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Update_Friend:
                try {
                    HashMap<String, Object> param = (HashMap<String, Object>) value;
                    int status = (int) param.get("status_friend");
                    isRefresh = status == 1 ||      // 添加好友
                            status == -4 ||         // 加入黑名单
                            status == -6;           // 删除好友
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case MsgType.MT_Msg_Unread:
                initBadgeView();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh) {
            isRefresh = false;
            onRefresh();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }
}
